package com.example.demo.scheduledtask;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.example.demo.common.Constant;
import com.example.demo.config.ErpConfig;
import com.example.demo.mapper.*;
import com.example.demo.utils.SoapXmlUtil;
import com.example.demo.vo.WarehouseTransferListReq;
import com.example.demo.vo.WarehouseTransferListVo;
import com.example.demo.vo.WarehouseTransferVo;
import com.example.demo.vo.XmlResp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author : Zhang.yt
 * @date : 2024/7/29
 */
@Component
@Slf4j
@DS("wms")
public class WarehouseTransferCallback {
    @Resource
    private WmsPurchaseMapper wmsPurchaseMapper;

    @Autowired
    private SoapXmlUtil soapXmlUtil;

    @Autowired
    PoMpsinfoMapper poMpsinfoMapper;

    @Resource
    private ErpConfig erpConfig;


    /**
     * 仓间调拨单回调
     */
//    @Scheduled(cron = "20 0/1 * * * ?")//开启定时同步
    @Scheduled(fixedDelay = 12 * 1000)//开启定时同步
    //@Async(value = "taskExecutor")
    public void warehouseTransferCallback() {
        //查询没有回调的数据
        WarehouseTransferVo warehouseTransferVo = wmsPurchaseMapper.getWarehouseTransferCallback();

        log.info("warehouseTransferVos:{}", warehouseTransferVo);
//        List<String> sbillnoList = warehouseTransferVos.stream().map(WarehouseTransferVo::getIndc003).collect(Collectors.toList());
        if (ObjectUtils.isEmpty(warehouseTransferVo)) {
            log.info("未查询到仓间调拨单回调数据，修改回写失败数据重新进行回写");
            wmsPurchaseMapper.warehouseTransferCallbackLose();
            return;
        }

        //根据没有回调的数据查询明细
        List<WarehouseTransferListVo> transferListVos = wmsPurchaseMapper.getWarehouseTransferListCallback(warehouseTransferVo.getIndc003());
        if (CollUtil.isEmpty(transferListVos)) {
            //发送soap消息，并解析结果
            wmsPurchaseMapper.updateWarehouseTransferCallback(warehouseTransferVo.getIndc003(), "没有明细资料,请确认是否已经回写！", 2);
            return;
        }
        try {
            //发送soap消息，并解析结果
            wmsPurchaseMapper.updateWarehouseTransferCallback(warehouseTransferVo.getIndc003(), "erp待返回", 3);
            warehouseTransferVo.setActionType(Constant.ActionType_I);
            warehouseTransferVo.setIndcdocno(Constant.PMDSDOCNO_1200);
            warehouseTransferVo.setIndcsite(Constant.PMDSSITE_AMA);
            List<WarehouseTransferListReq> warehouseTransferListReqs = BeanUtil.copyToList(transferListVos, WarehouseTransferListReq.class);
            String resMessage = soapXmlUtil.getSoapXml("WarehouseTransfer", "indc_t", "indd_t", warehouseTransferVo, warehouseTransferListReqs);

            //转换为soap消息
            log.info("仓间调拨单回调：{}", resMessage);
            String result;
            try {
                result = soapXmlUtil.sendSoapMessageNew(resMessage, erpConfig.getUrl());
            } catch (Exception e) {
                result = soapXmlUtil.sendSoapMessageNew(resMessage, erpConfig.URL2);
            }
            //调用等待
            if (warehouseTransferListReqs.size() >= 50) {
                Thread.sleep(40 * 1000);
//                TimeUnit.SECONDS.sleep(600 * 1000);
            } else if (warehouseTransferListReqs.size() >= 30) {
                Thread.sleep(20 * 1000);
//                TimeUnit.SECONDS.sleep(30 * 1000);
            } else if (warehouseTransferListReqs.size() >= 10) {
                Thread.sleep(5 * 1000);
//                TimeUnit.SECONDS.sleep(10 * 1000);
            }
            log.info("仓间调拨单回调结果: {}", result);
            XmlResp xmlResp = soapXmlUtil.analysisXml(result);
            if (xmlResp.getCode().equals("0")) {
                //更新相应表状态为已下发
                wmsPurchaseMapper.updateWarehouseTransferCallback(warehouseTransferVo.getIndc003(), xmlResp.getData().get("indcdocno"), 1);
            } else {
                wmsPurchaseMapper.updateWarehouseTransferCallback(warehouseTransferVo.getIndc003(), xmlResp.getMessage(), 2);
            }
        } catch (Exception e) {
            wmsPurchaseMapper.updateWarehouseTransferCallback(warehouseTransferVo.getIndc003(), "非常见的数据处理问题，请联系开发人员处理", 2);
            e.printStackTrace();
        }
//        finally {
//            log.error("finally释放锁{}", this.getClass().getName());
//            if (ObjectUtil.isNotEmpty(lock) && lock.isHeldByCurrentThread()) {
//                lock.unlock();
//            }
//        }

    }
}
